ihelpuoo.namespace("ihelpuoo.widget.drop");

var ihelpuoo_widget_drops = null;

ihelpuoo.widget.drop.hideAll = function()
{
	for(var i=0;i<ihelpuoo_widget_drops.length;i++)
		ihelpuoo_widget_drops[i].retract();
}

ihelpuoo.widget.drop.DropView = function(gasket
										,name
										,ptnName
										,subPtnName
										,fClick
										,fDrawSub)
{
	if(!ihelpuoo_widget_drops)
	{
		ihelpuoo_widget_drops = new Array();
		ihelpuoo.event.add(document.body,"click",ihelpuoo.widget.drop.hideAll);
	}
	
	// Attributes ...
	this.gasket = gasket;
	this.name = name;
	this.ptnName = (null==ptnName?"wares.pattern.drop.DropShell":ptnName);
	this.subPtnName = (null==subPtnName?"wares.pattern.drop.DropItem":subPtnName);
	this.fClick = fClick;
	this.fDrawSub = fDrawSub;
	if(this.name)
		ihelpuoo.widget.registerView2(this);
	ihelpuoo_widget_drops.push(this);
	
}

ihelpuoo.widget.drop.DropView.prototype
=
{
	isNeverBeDrawn: function()
	{
		if(!this.view) return true;
		return this.view.isNeverBeDrawn();
	}
	,
	preparePattern: function()
	{
		if(!this.view)
			this.view = new ihelpuoo.widget.View(this.gasket,this.ptnName);
		this.view.preparePattern();
	}
	
	,
	draw : function(list , aIndex)
	{
		if(this.isNeverBeDrawn())
		{
			this.preparePattern();
			this.oTitle = this.view.getElement("title");
			this.oIcon = this.view.getElement("icon");
			this.oArena = this.view.getElement("arena");
			ihelpuoo.widget.addViewEvent(this,this.oIcon,"click",this.onDrop);
		}
		this.view.display();
		this.list = list;
	
		if(null==list || !list.length || list.length==0 || !this.oArena || !this.oTitle)
			return;
		ihelpuoo.dom.clearAllChildren(this.oArena);
		this.children = new Array();
		
		for(var i=0;i<list.length;i++)
		{
			this.children.push(this._createItem(this.oArena,list,i));
		}
		this.setActive(aIndex ? aIndex : 0);
	}
	
	,
	_createItem: function(gasket,list,index)
	{
		var v = new ihelpuoo.widget.View(gasket,this.subPtnName);
		v.parent = this;
		v.index = index;
		if(!this.fDrawSub)
			v.draw(list[index]);
		else
		{
			v.preparePattern();
			v.display();
		}
		var goon = true;
		if(this.fDrawSub) goon = this.fDrawSub(v,index);
		if(goon)
		{
			var keys = v.pattern.keys();
			for(var i=0;i<keys.length;i++)
			{
				var ele = v.getElement(keys[i]);
				ihelpuoo.widget.addViewEvent(this,ele,"click",this.onClick);
				ele.setAttribute("my-index",index);
			}
		}
		return v;
	}
	
	,
	onClick: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		var ele = ihelpuoo.event.srcElement(this);
		var v = ihelpuoo.widget.getViewByElement(ele);
		var index = parseInt(ele.getAttribute("my-index"));
		if(v.fClick)
		{
			if(v.fClick(v,index))
				v.setActive(index);
		}
		else
			v.setActive(index);
		v.retract();
	}
	
	,
	setActive: function(index)
	{
		if(index<0) return;
		if(index>=this.list.length) return;
		
		if(this.actived && this.actived.index==index) return;

		ihelpuoo.dom.clearAllChildren(this.oTitle);
		this.actived = this._createItem(this.oTitle,this.list,index);
	}
	
	,
	onDrop: function()
	{
		ihelpuoo.event.cancelBubble(ihelpuoo.Event(arguments));
		var v = ihelpuoo.widget.getViewByEventSource(this);
		v.drop();
	}
	
	,
	drop: function()
	{
		ihelpuoo.dom.showElement(this.oArena);
	}
	
	,
	retract: function()
	{
		this.oArena.style.display = "none";
	}
	
	// <WEBOO:INHERIT extends="ihelpuoo.widget.View">
	,getElement: function(key){return this.view.getElement(key);}
	,display: function(){this.view.display();}
	,hide: function(){this.view.hide();}
	// </WEBOO:INHERIT>
}